/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package apus.action.phonenumber;

import apus.action.Action;
import apus.action.ActionResult;
import apus.authorisation.UserAuthorisation;
import apus.entity.PhoneNumber;
import apus.entity.User;
import apus.persistence.OrganizationDAO;
import apus.persistence.PersonDAO;
import apus.persistence.PhoneNumberDAO;
import apus.persistence.UserDAO;
import apus.services.ServiceLocator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author 21vek
 */
public class PhoneNumberAction implements Action {

    @Override
    public String getName() {
        return "phoneNumber.view";
    }

    @Override
    public ActionResult execute(HttpServletRequest request, HttpServletResponse response) {
        String userLogin = ((UserAuthorisation) request.getSession().getAttribute("userAuth")).getLogin();
        UserDAO udao = ServiceLocator.getService(UserDAO.class);
        User user = udao.findByLogin(userLogin);
        if (request.getParameter("id") != null) {
            int id = Integer.parseInt(request.getParameter("id"));
            PhoneNumber phoneNumber = ServiceLocator.getService(PhoneNumberDAO.class).read(id);
            int phone_sub_id = phoneNumber.getOwner().getId();
            //if user
            if (((UserAuthorisation) request.getSession().getAttribute("userAuth")).getRole() == UserAuthorisation.Role.USER) {
                int sub_id = user.getSubscriber().getId();
                if (sub_id != phone_sub_id) {
                    if (user.getSubscriber().getSubscriberType().equals("person")) {
                        request.setAttribute("person", ServiceLocator.getService(PersonDAO.class).read(sub_id));
                    } else {
                        request.setAttribute("organization", ServiceLocator.getService(OrganizationDAO.class).read(sub_id));
                    }
                    return ActionResult.redirect("main.view");
                }
            }

            request.setAttribute("phoneNumber", phoneNumber);
            return ActionResult.forward("/WEB-INF/View/PhoneNumberView/PhoneNumberView.jsp");
        } else {
            if (((UserAuthorisation) request.getSession().getAttribute("userAuth")).getRole() == UserAuthorisation.Role.USER) {
                int sub_id = user.getSubscriber().getId();
                if (user.getSubscriber().getSubscriberType().equals("person")) {
                    request.setAttribute("person", ServiceLocator.getService(PersonDAO.class).read(sub_id));
                } else {
                    request.setAttribute("organization", ServiceLocator.getService(OrganizationDAO.class).read(sub_id));
                }
                return ActionResult.redirect("main.view");
            }
            List<PhoneNumber> phoneNumberList = ServiceLocator.getService(PhoneNumberDAO.class).findAll();
            request.setAttribute("phoneNumberList", phoneNumberList);
            return ActionResult.redirect("phoneNumber.list");
        }

    }
}
